﻿using Learun.Application.TwoDevelopment.ZZDT_EC;
using Learun.Util;
using Learun.Util.SqlSugar;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.Mvc;

namespace Learun.Application.Web.Areas.ZZDT_EC.Controllers
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-03-07 13:44
    /// 描 述：工程数据表
    /// </summary>
    public class ec_enginedataController : MvcControllerBase
    {
        private ec_enginedataIBLL ec_enginedataIBLL = new ec_enginedataBLL();

        #region 视图功能

        /// <summary>
        /// 主页面
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 表单页
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Form()
        {
            return View();
        }
        /// <summary>
        /// 工程数据回收站
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult RecycleBin()
        {
            return View();
        }
        #endregion

        #region 获取数据

        /// <summary>
        /// 获取页面显示列表数据
        /// <summary>
        /// <param name="queryJson">查询参数</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetPageList(string pagination, string queryJson)
        {
            Pagination paginationobj = pagination.ToObject<Pagination>();
            var data = ec_enginedataIBLL.GetList(queryJson, paginationobj, true, true);
            var jsonData = new
            {
                rows = data,
                total = paginationobj.total,
                page = paginationobj.page,
                records = paginationobj.records
            };
            return Success(jsonData);
        }

        /// <summary>
        /// 获取回收站页面显示列表数据
        /// <summary>
        /// <param name="queryJson">查询参数</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetRecycleBinPageList(string pagination, string queryJson)
        {
            Pagination paginationobj = pagination.ToObject<Pagination>();
            var data = ec_enginedataIBLL.GetRecycleBinPageList(paginationobj, queryJson);
            var jsonData = new
            {
                rows = data,
                total = paginationobj.total,
                page = paginationobj.page,
                records = paginationobj.records
            };
            return Success(jsonData);
        }

        /// <summary>
        /// 获取表单数据
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetFormData(string keyValue, string ProjectId)
        {
            var ec_enginedataData = ec_enginedataIBLL.GetEntity(keyValue, ProjectId);
            var jsonData = new
            {
                ec_enginedata = ec_enginedataData,
            };
            return Success(jsonData);
        }
        #endregion

        #region 提交数据

        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult DeleteForm(string strEntity, string ProjectId)
        {
            var entitys = strEntity.ToObject<List<ec_enginedataEntity>>();
            var ids = entitys.Select(x => x.EngineDataID).ToList();
            var tableName = ProjectSugar.TableName<ec_CableEntity>(ProjectId);
            var cables = SqlSugarHelper.Db.Queryable<ec_CableEntity>().AS(tableName).Where(x => ids.Contains(x.EngineerDataID)).ToList();
            string res = "";
            cables.ForEach(x =>
            {
                var entity = entitys.First(y => x.EngineerDataID == y.EngineDataID);
                res += $"位号{entity.TagNumber}，已绑定电缆,无法删除\n";
            });
            if (!string.IsNullOrEmpty(res))
            {
                return Fail(res);
            }
            foreach (var item in entitys)
            {
                ec_enginedataIBLL.DeleteEntity(item.EngineDataID, ProjectId);
            }
            return Success("删除成功！");
        }
        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        public ActionResult SaveForm(string keyValue, string strEntity, string ProjectId)
        {
            ec_enginedataEntity entity = strEntity.ToObject<ec_enginedataEntity>();
            //位号不允许重复
            var data = ec_enginedataIBLL.GetList("{ProjectId:\"" + ProjectId + "\",TagNumber:\"" + entity.TagNumber + "\"}").ToList();
            if (data != null && data.Count > 0)
            {
                if (!string.IsNullOrEmpty(keyValue))
                {
                    data = data.FindAll(x => x.EngineDataID != keyValue);
                    if (data != null && data.Count > 0)
                    {
                        return Fail("位号不允许重复！");
                    }
                }
                else
                {
                    return Fail($"位号不允许重复！在{data[0].FullPathCN}中有一个相同位号的设备或电缆。");
                }
            }
            //截取位号后面的数字作为流水号
            if (!string.IsNullOrEmpty(entity.ObjectTypeNameEN))
            {
                string strSerialNumber = entity.TagNumber.Replace(entity.ObjectTypeNameEN, "").Trim();
                if (IsInteger(strSerialNumber))
                {
                    entity.SerialNumber = strSerialNumber.ToInt();
                }
                else
                {
                    entity.SerialNumber = 0;
                }
            }
            ec_enginedataIBLL.SaveEntity(keyValue, entity, ProjectId);
            return Success("保存成功！");
        }

        /// <summary>
        /// 验证字符串是否为整数
        /// </summary>
        /// <param name="value">要验证的字符串</param>    
        public static bool IsInteger(string value)
        {
            //如果为空，认为验证不合格
            if (string.IsNullOrEmpty(value))
            {
                return false;
            }
            return System.Text.RegularExpressions.Regex.IsMatch(value, @"^\d*$");
        }

        /// <summary>
        /// 批量修改数据状态
        /// <summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult UpdateDataStatus(string strEntity, string ProjectId)
        {
            List<ec_enginedataEntity> entityList = strEntity.ToList<ec_enginedataEntity>();
            ec_enginedataIBLL.UpdateDataStatus(entityList, ProjectId);
            return Success("审批成功！");
        }
        #endregion

    }
}
